//
//  launchPlatSprite.m
//  StarBugs
//
//  Created by ?? ? on 12/29/11.
//  Copyright 2011 antisword@playorca.com. All rights reserved.
//

#include "Sprite/launchPlatSprite.h"
#include "Util/ImageManager.h"
#include "Util/SoundManager.h"
#include "Util/ModuleController.h"
using namespace cocos2d;

#pragma mark LineSprite
LineSprite::~LineSprite()
{
    
}

LineSprite::LineSprite()
{
    
}
void LineSprite::draw()
{
//    glEnable(GL_LINE_SMOOTH);
    glLineWidth(3 * CCDirector::sharedDirector()->getContentScaleFactor() * ModuleController::getImageScale());
    ccDrawColor4B(0, 0, 0, 255);
    ccDrawLine(point1, point2);

}

void LineSprite::setPos(cocos2d::CCPoint pt1, cocos2d::CCPoint pt2)
{
    point1 = pt1;
    point2 = pt2;
}

#pragma mark LaunchPlatSprite

LaunchPlatSprite::~LaunchPlatSprite()
{
    
}

LaunchPlatSprite::LaunchPlatSprite(CCPoint pt)
{
    

    mainSprite = new CCSprite();
    mainSprite->init();
    mainSprite->autorelease();
    addChild(mainSprite);
    mainPos = pt;
    CCSprite *sprite;
    
    lineSprite[0] = new LineSprite;
    lineSprite[0]->autorelease();
    lineSprite[0]->setPos(ccp(0, -22 * ModuleController::getImageScale()), ccp(- 10 * ModuleController::getImageScale(), - 23 * ModuleController::getImageScale()));
    mainSprite->addChild(lineSprite[0]);
    
    lineSprite[1] = new LineSprite;
    lineSprite[1]->autorelease();
    lineSprite[1]->setPos(ccp(0, -22 * ModuleController::getImageScale()), ccp(10 * ModuleController::getImageScale(), - 23 * ModuleController::getImageScale()));
    mainSprite->addChild(lineSprite[1]);
    
    arrowSprite = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "cannon_arrow");
    arrowSprite->setPosition(ccp(0, -23));
    mainSprite->addChild(arrowSprite);
    


    bodySprite = new CCSprite();
    bodySprite->init();
    bodySprite->autorelease();
    addChild(bodySprite);
    sprite = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "cannon_body");
    sprite->setPosition(ccp(0,6));
    bodySprite->addChild(sprite);
    
    
    bodySprite->setPosition(pt);
    mainSprite->setPosition(pt);
    
    shotLineSprite = ImageManager::getSpriteWithXml2(kImagesheet_GameSource, "cannon_shotline");
//    shotLineSprite->setScaleY(0.1);
    shotLineSprite->setAnchorPoint(ccp(0.5, 0));
    shotLineSprite->setPosition(ccp(0, 37));
    mainSprite->addChild(shotLineSprite);
    shotLineSprite->setVisible(false);
}

void LaunchPlatSprite::returnAction()
{
    CCRotateTo *rotate = CCRotateTo::create(0.5, 0);
    mainSprite->runAction(rotate);
    rotate = CCRotateTo::create(0.5, 0);
    bodySprite->runAction(rotate);
}

void LaunchPlatSprite::showAction()
{
    mainSprite->stopAllActions();
    bodySprite->stopAllActions();
    
    mainSprite->setRotation(0);
    bodySprite->setRotation(0);
    
    CCScaleTo *scale = CCScaleTo::create(0.2, 1.2);
    CCScaleTo *scale2 = CCScaleTo::create(0.1, 0.8);
    CCScaleTo *scale3 = CCScaleTo::create(0.1, 1.0);
    
    CCAction *action = CCSequence::create(scale, scale2, scale3, NULL);
    mainSprite->runAction(action);
    
    scale = CCScaleTo::create(0.2, 1.2);
    scale2 = CCScaleTo::create(0.1, 0.8);
    scale3 = CCScaleTo::create(0.1, 1.0);
    action = CCSequence::create(scale, scale2, scale3, NULL);
    bodySprite->runAction(action);
}

void LaunchPlatSprite::shotAction()
{
    CCScaleTo *scale = CCScaleTo::create(0.2, 1.3, 0.7);
    CCScaleTo *scale2 = CCScaleTo::create(0.1, 0.7, 1.3);
    CCScaleTo *scale3 = CCScaleTo::create(0.1, 1.2, 0.8);
    CCScaleTo *scale4 = CCScaleTo::create(0.1, 0.9, 1.1);
    CCScaleTo *scale5 = CCScaleTo::create(0.1, 1.0, 1.0);
    CCCallFunc *call = CCCallFunc::create(this, callfunc_selector(LaunchPlatSprite::_callHide));
    CCAction *action = CCSequence::create(scale, scale2, scale3, scale4, scale5,call, NULL);
    bodySprite->runAction(action);

}

void LaunchPlatSprite::hideAction()
{
    CCScaleTo *scale2 = CCScaleTo::create(0.2, 0);
    CCAction *action = CCSequence::create( scale2, NULL);
    mainSprite->runAction(action);
    
//    scale2 = CCScaleTo::create(0.2, 0);
//    action = CCSequence::create( scale2, NULL);
//    rackSprite->runAction(action);
    
    scale2 = CCScaleTo::create(0.2, 0);
    action = CCSequence::create( scale2, NULL);
    bodySprite->runAction(action);
}

void LaunchPlatSprite::setVisableShotLine(bool flg)
{
    shotLineSprite->setVisible(flg);
}

void LaunchPlatSprite::_callHide()
{
//    hideAction();
    
    returnAction();
}

void LaunchPlatSprite::setAngle(float angle)
{
//    CCLog("%f", angle);
    mainSprite->setRotation(angle);
    bodySprite->setRotation(angle);
}

void LaunchPlatSprite::setScale(float scale)
{
    mainSprite->setScale(scale);
}

void LaunchPlatSprite::setDistance(float dist)
{
    float tmp;
    if(dist <= 23)
        tmp = -23;
    else if(dist > 23 && dist < 70)
        tmp = -dist;
    else if(dist >= 70)
        tmp = -70;
    
    arrowSprite->setPosition(ccp(0, tmp));
    lineSprite[0]->setPos(ccp(0, -22 * ModuleController::getImageScale()), ccp(- 10 * ModuleController::getImageScale(), tmp * ModuleController::getImageScale()));
    lineSprite[1]->setPos(ccp(0, -22 * ModuleController::getImageScale()), ccp( 10 * ModuleController::getImageScale(), tmp * ModuleController::getImageScale()));
    
    shotLineSprite->setScaleY(1 + (-tmp - 23) / 47.f);

}
